package com.yidu.DatelessManagement.service.impl;

import com.yidu.DatelessManagement.entity.InventoryStatistics;
import com.yidu.DatelessManagement.mapper.InventoryStatisticsMapper;
import com.yidu.DatelessManagement.service.InventoryStatisticsService;
import com.yidu.DatelessManagement.util.*;
import com.yidu.GlobalManagement.log.util.DbUtil;
import com.yidu.InventoryManagement.entity.*;
import com.yidu.InventoryManagement.mapper.*;
import com.yidu.SystemManagement.util.TimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 库存统计
 * @author :yimeng
 * @date :2023/2/1  11:26
 * @description:some description
 */
@Service
public class InventoryStatisticsImpl implements InventoryStatisticsService {
    //注入库存统计Mapper放法
    @Resource
    private InventoryStatisticsMapper isMapper;
    //证券库存
    @Resource
    private SecurityStockMapper securityStockMapper;
    //现金库存
    @Resource
    private CashStockMapper cashStockMapper;
    //TA库存
    @Resource
    private TAStockMapper taStockMapper;
    //证券应收应付库存
    @Resource
    private SecurityAccrualStockMapper securityAccrualStockMapper;
    //现金应收应付库存
    @Resource
    private CashAccrualStockMapper cashAccrualStockMapper;



    //查询库存
    @Override
    public List<InventoryStatistics> selectInventoryStatistics(String fundId,String time,String dealTime,String invId) {
        //创建List保持库存统计信息
        List<InventoryStatistics>  list=new ArrayList<InventoryStatistics>();
        TimeUtil timeUtil = new TimeUtil();
        //库存信息
        InventoryStatistics securitiesInventory=new InventoryStatistics(1,"证券库存",null,"admin","",0,"暂无");
        InventoryStatistics cashInventory=new InventoryStatistics(2,"现金库存",null,"admin","",0,"暂无");
        InventoryStatistics taInventory=new InventoryStatistics(3,"TA库存",null,"admin","",0,"暂无");
        InventoryStatistics securitiesClosedPayInventory=new InventoryStatistics(4,"证券应收应付库存",null,"admin","",0,"暂无");
        InventoryStatistics cashClosedPayInventory=new InventoryStatistics(5,"现金应收应付库存",null,"admin","",0,"暂无");
        //判断要统计的库存
        if (invId != null && !invId.equals("")){
            String[] invIdList=null;
            invIdList=invId.split(",");

            for (String strInvId : invIdList){
                switch (strInvId){
                    //证券库存
                    case "1": ;
                        List<SecurityDael> securityStocks = isMapper.selectSecurityStock(time,dealTime,fundId);
                        securitiesInventory = new InventoryStatistics(1,"证券库存",fundId,"admin",dealTime,securityStocks.size(),"已统计");
                        //遍历
                        if (securityStocks.size() != 0 && securityStocks.get(0)!= null){
                            for (SecurityDael securityStock : securityStocks) {
                                //删除证券库存信息
                                isMapper.deleteSecurityStock(securityStock.getSecurityId(),fundId);
                                //增加证券库存数据
                                SecurityStock securityStock1 = new SecurityStock();
                                //证券库存ID
                                String s = isMapper.selectSecurityDealMaxId(securityStock.getSecurityId(), fundId);
                                if(s!=null){
                                    securityStock1.setSecurityStockId(s);
                                }else{
                                    securityStock1.setSecurityStockId(securityStock.getSecurityId());
                                }
                                //证券库存日期    date
                                securityStock1.setDate(dealTime);
                                //证券信息表ID   securityId
                                securityStock1.setSecurityId(securityStock.getSecurityId());
                                //基金表ID fundId
                                securityStock1.setFundId(fundId);
                                //是否导入其他系统数据    initialState
                                securityStock1.setInitialState(0);
                                //证券的数量 quantity
                                securityStock1.setQuantity(securityStock.getQuantity());
                                //单位成本  unitCost
                                securityStock1.setUnitCost(securityStock.getUnitCost());
                                //总金额   money
                                securityStock1.setMoney(securityStock.getMoney());
                                //备注
                                securityStock1.setRemark("证券库存统计");
                                //调用增加方法
                                securityStockMapper.insertSecurityStock(securityStock1);
                            }
                        }
                        break;
                    //现金库存
                    case "2":
                        List<CashStockDael> cashStocks = isMapper.selectCashStock(time,dealTime,fundId);
                        cashInventory = new InventoryStatistics(2,"现金库存",fundId,"admin",dealTime,cashStocks.size(),"已统计");
                        //遍历
                        if (cashStocks.size() !=0 && cashStocks.get(0) != null){
                            for (CashStockDael cashStock : cashStocks){
                                //删除原现金库存信息
                                isMapper.deleteCashStock(cashStock.getCashStockId(), fundId);
                                //增加新数据
                                CashStock cashStock1 = new CashStock();
                                //现金库存Id    cashStockId()
                                String s = isMapper.selectCashStockDealMaxId(cashStock.getCashStockId(),fundId);
                                if(s!=null){
                                    cashStock1.setCashStockId(s);
                                }else{
                                    cashStock1.setCashStockId(cashStock.getCashStockId());
                                }
                                //基金Id
                                cashStock1.setFundId(fundId);
                                //现金余额
                                cashStock1.setMoney(cashStock.getMoney());
                                //现金账户ID
                                cashStock1.setCashId(cashStock.getCashId());
                                //统计日期
                                cashStock1.setDate(dealTime);
                                //备注
                                cashStock1.setRemark("统计现金库存");
                                //调用增加方法
                                cashStockMapper.insertCashStock(cashStock1);
                            }
                        }
                        break;
                    //TA库存
                    case "3":
                        List<TaStockDael> taStock = isMapper.selectTa(time,dealTime,fundId);
                        taInventory = new InventoryStatistics(3,"TA库存",fundId,"admin",dealTime,taStock.size(),"已统计");
                        //遍历
                        if(taStock.size()!=0 && taStock.get(0)!=null) {
                            for (TaStockDael taStockDael : taStock) {
                                //根据日期删除原TA库存信息
                                isMapper.deleteTaStock(taStockDael.getSecurityAccrualStockId(), fundId);
                                //新建TA库存
                                TAStock taStock1 = new TAStock();
                                //TA库存ID
                                String s = isMapper.selectTaStockMaxId(taStockDael.getSecurityAccrualStockId(),fundId);
                                if(s!=null){
                                    taStock1.setSecurityAccrualStockId(s);
                                }else{
                                    taStock1.setSecurityAccrualStockId(taStockDael.getSecurityAccrualStockId());
                                }
                                //基金ID
                                taStock1.setFundId(fundId);
                                //Ta数量
                                taStock1.setQuantity(taStockDael.getQuantity());
                                //总金额
                                taStock1.setMoney(taStockDael.getMoney());
                                //统计日期
                                taStock1.setDate(dealTime);
                                //是否从其他系统导入的期初数据  2：不是  1：是
                                taStock1.setInitialState(2);
                                //备注
                                taStock1.setRemarks("统计Ta库存");
                                taStock1.setRemark("统计Ta库存");
                                //新增
                                taStockMapper.insertTaShockk(taStock1);
                            }
                        }
                        break;
                    //证券应收应付库存
                    case "4":
                        List<SecurityAccrualStockDael> sasd = isMapper.selectSecurityAccrualStock(time, dealTime, fundId);
                        securitiesClosedPayInventory = new InventoryStatistics(4,"证券应收应付库存",fundId,"admin",dealTime,sasd.size(),"已统计");
                        //遍历
                        if(sasd.size()!=0 && sasd.get(0)!=null) {
                            for (SecurityAccrualStockDael stockDael : sasd) {
                                //根据日期删除
                                isMapper.deleteSecurityAccrualStock(stockDael.getSecurityAccrualStockId(), fundId);
                                //定义新证券应收应付库存对象
                                SecurityAccrualStock securityAccrualStock = new SecurityAccrualStock();
                                //证券应收应付ID
                                String s = isMapper.selectSecurityAccrualStockMaxId(stockDael.getSecurityAccrualStockId(), fundId);
                                if(s!=null){
                                    securityAccrualStock.setSecurityAccrualStockId(s);
                                }else{
                                    securityAccrualStock.setSecurityAccrualStockId(stockDael.getSecurityAccrualStockId());
                                }
                                //业务时间
                                securityAccrualStock.setDate(dealTime);
                                //基金Id
                                securityAccrualStock.setFundId(fundId);
                                //证券ID
                                securityAccrualStock.setSecurityId(stockDael.getSecurityId());
                                //证券应收应付类型 1=估值款 2=证券清算款 3=债券利息
                                securityAccrualStock.setSecurityType(3);
                                //业务状态 1流入，-1流出
                                securityAccrualStock.setBusinessState(1);
                                //总金额
                                securityAccrualStock.setMoney(stockDael.getMoney());
                                //期初标志 是否从其他系统导入得期初数据 2：不是 1：是
                                securityAccrualStock.setInitialState(2);
                                //备注
                                securityAccrualStock.setRemark("证券应收应付库存统计");
                                //增加
                                securityAccrualStockMapper.addSecuritiesInventoryInfo(securityAccrualStock);
                            }
                        }
                        break;
                    //现金应收应付库存
                    case "5":
                        List<CashAccrualStockDael> casd = isMapper.selectCashAccrualStock(time,dealTime,fundId);
                        cashClosedPayInventory = new InventoryStatistics(5,"现金应收应付库存",fundId,"admin",dealTime,casd.size(),"已统计");
                        //遍历
                        if (casd.size()!=0 && casd.get(0)!=null){
                            for (CashAccrualStockDael cashAccrualStockDael : casd) {
                                //根据日期删除
                                int i = isMapper.deleteCashAccrualStock(cashAccrualStockDael.getCashAccrualStockId(), time);
                                //定义新现金应收应付库存对象
                                CashAccrualStock cashAccrualStock = new CashAccrualStock();
                                //现金应收应付库存Id
                                String s = isMapper.selectCashAccrualStockMaxId(cashAccrualStockDael.getCashAccrualStockId());
                                if(s!=null){
                                    cashAccrualStock.setCashAccrualStockId(s);
                                }else{
                                    cashAccrualStock.setCashAccrualStockId(cashAccrualStockDael.getCashAccrualStockId());
                                }
                                //业务日期
                                cashAccrualStock.setDate(dealTime);
                                //现金账户ID
                                cashAccrualStock.setCashId(cashAccrualStockDael.getCashId());
                                //业务类型  1.管理费  2.托管费  3.存款利息  4.申购赎回费
                                cashAccrualStock.setBusinessType(1);
                                //业务状态   1.流入  -1.流出
                                cashAccrualStock.setBusinessState(1);
                                //期初标志  1.是   0.否
                                cashAccrualStock.setInitialState(1);
                                //总金额
                                cashAccrualStock.setMoney(cashAccrualStockDael.getMoney());
                                //备注
                                cashAccrualStock.setRemark("现金应收应付库存");
                                //增加
                                cashAccrualStockMapper.addCashAccrualStock(cashAccrualStock);
                            }
                        }
                        break;
                }
            }

        }


        //库存信息添加到list
        list.add(securitiesInventory);
        list.add(cashInventory);
        list.add(taInventory);
        list.add(securitiesClosedPayInventory);
        list.add(cashClosedPayInventory);
        return list;
    }





}
